package sourceagile.webservices.github;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jettison.json.JSONObject;

import sourceagile.server.githubFileRepository.GetGithubFileContent;
import sourceagile.webservices.GetCallback;

@SuppressWarnings("serial")
public class GetGithubFileContentServlet extends HttpServlet {

	// http://localhost:8080/LiveSourceWebServices/GetGithubFileContentService
	// ?projectName=TravelLog
	// &username=allineo
	// &sha=c2278fdc2f83aad7943a350d3d37ad7dd6d0ef63

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String projectName = request.getParameter("projectName");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String objectSHA = request.getParameter("sha");

		JSONObject fileContent = GetGithubFileContent.jsonFileContent(username,
				projectName, objectSHA);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), fileContent.toString());

		response.setContentType("text/javascript");

		response.getWriter().println(answer);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String projectName = request.getParameter("projectName");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String objectSHA = request.getParameter("sha");

		JSONObject fileContent = GetGithubFileContent.jsonFileContent(username,
				projectName, objectSHA);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), fileContent.toString());

		response.setContentType("text/javascript");

		response.getWriter().println(answer);

	}
}
